﻿Imports VN.INS.BL.BC
Imports System.Data
Imports Microsoft.Office.Interop
Imports System.IO

Partial Class Devida_ListaAvancesMensual
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If (Not (Page.IsPostBack)) Then

            ViewState("Persona") = ""
            Dim dtUsuarios As System.Data.DataTable = New System.Data.DataTable
            ListaTipo()
            ListaPeriodo()
            CargarGrilla("", "all", ddlPeriodo.SelectedValue.ToString)
        End If
    End Sub

    'cargar grilla
    Public Sub CargarGrilla(ByVal strNombre As String, ByVal strTipo As String, ByVal strPeriodo As String)
        Dim objPOABC As ReporteBC
        Dim objData As DataTable
        objPOABC = New ReporteBC
        'Si no es administrador capturar codigo de persona
        If Session("perfil") <> "AD" Then
            ViewState("Persona") = Session("persona")
        End If
        'cargar data
        objData = objPOABC.ListaPOA_Mensual(strNombre, strTipo, ViewState("Persona"), strPeriodo)
        'cargar pagina
        If Session("AFDetalle") = 1 Then
            If Session("Pagina") IsNot Nothing Then
                gv_impresion.PageIndex = Session("Pagina")
            End If
            Session("AFDetalle") = 0
        End If
        'si hay datos caargar grilla
        If ViewState("filas") > 0 Then
            gv_impresion.DataSource = objData
            gv_impresion.DataBind()

            gv_doc.DataSource = objData
            gv_doc.DataBind()
        End If
    End Sub

    Public Sub ListaTipo()
        Dim objMonitorBC As MONITOREOBC = New MONITOREOBC
        Dim dttip As DataTable
        dttip = objMonitorBC.tipoProyectoActividad()
        Dim drttiprow As DataRow = dttip.NewRow()
        drttiprow("DESCRIPCION") = "--Seleccione--"
        drttiprow("TIPO_PROYECTO_ACTIVIDAD") = "all"
        dttip.Rows.Add(drttiprow)
        ddlTipo.DataSource = dttip
        ddlTipo.DataTextField = "DESCRIPCION"
        ddlTipo.DataValueField = "TIPO_PROYECTO_ACTIVIDAD"
        ddlTipo.DataBind()
        ddlTipo.SelectedValue = "all"
    End Sub

    Public Sub ListaPeriodo()
        Dim objGeneral As GeneralBC
        Dim objData As DataTable
        objGeneral = New GeneralBC
        objData = New DataTable
        objData = objGeneral.ListaPeriodoMensual("", "DESC", Session("Año"))
        ddlPeriodo.DataSource = objData
        ViewState("filas") = objData.Rows.Count
        ddlPeriodo.DataTextField = "DESCRIPCION"
        ddlPeriodo.DataValueField = "PERIODO"
        ddlPeriodo.DataBind()
    End Sub

    'salir
    Protected Sub ibn_salir_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ibn_salir.Click
        Session("Pagina") = Nothing
        Response.Redirect("Inicio.aspx")
    End Sub

    Protected Sub buscar_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles buscar.Click
        CargarGrilla(txt_numero.Text.ToString, ddlTipo.SelectedValue.ToString, ddlPeriodo.SelectedValue.ToString)
    End Sub

    Protected Sub gv_impresion_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gv_impresion.PageIndexChanging
        'seleccion de pagina
        Dim newPageNumber As Integer
        If Session("Pagina") IsNot Nothing Then
            newPageNumber = Session("Pagina")
        Else
            newPageNumber = e.NewPageIndex + 1
        End If
        gv_impresion.PageIndex = e.NewPageIndex
        Session("Pagina") = e.NewPageIndex
        CargarGrilla(txt_numero.Text.ToString, ddlTipo.SelectedValue.ToString, ddlPeriodo.SelectedValue.ToString)
        gv_impresion.SelectedIndex = -1
    End Sub

    Protected Sub gv_impresion_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv_impresion.RowDataBound
        'llenado de celdas
        If e.Row.RowIndex >= 0 Then
            Select Case e.Row.Cells(6).Text
                Case 1
                    e.Row.Cells(6).BackColor = Drawing.Color.FromArgb(0, 128, 0)
                Case 2
                    e.Row.Cells(6).BackColor = Drawing.Color.FromArgb(250, 150, 0)
                Case 3
                    e.Row.Cells(6).BackColor = Drawing.Color.FromArgb(250, 50, 50)
                Case 4
                    e.Row.Cells(6).BackColor = Drawing.Color.White
            End Select
            e.Row.Cells(6).Text = ""
            If e.Row.Cells(7).Text = "&nbsp;" Then
                e.Row.Cells(7).Text = "Falta ingresar"
                e.Row.Cells(7).ForeColor = Drawing.Color.Red
            Else
                e.Row.Cells(7).Text = "Ingresado"
            End If
        End If

        ' FORMATEA ROWS
        If e.Row.RowType = DataControlRowType.DataRow Then
            ' ASIGNA EVENTOS
            e.Row.Attributes.Add("OnMouseOver", "Resaltar_On(this);")
            e.Row.Attributes.Add("OnMouseOut", "Resaltar_Off(this);")
            e.Row.Attributes("OnClick") = Page.ClientScript.GetPostBackClientHyperlink(Me.gv_impresion, "Select$" + e.Row.RowIndex.ToString)
        End If
    End Sub

    Protected Sub gv_impresion_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gv_impresion.SelectedIndexChanged
        Session("Pagina") = gv_impresion.PageIndex
        'captura de variables de fila seleccionada
        Dim hfPOA As HiddenField = CType(gv_impresion.SelectedRow.FindControl("POA"), HiddenField)
        Dim strPPER As Label = CType(gv_impresion.SelectedRow.FindControl("lblPPER"), Label)
        Dim strEjecutor As String = gv_impresion.SelectedRow.Cells(2).Text.ToString
        Dim hfPPER As HiddenField = CType(gv_impresion.SelectedRow.FindControl("hfPPER"), HiddenField)
        Dim hfEjecutor As HiddenField = CType(gv_impresion.SelectedRow.FindControl("hfEjecutor"), HiddenField)
        Dim strNombre As String = gv_impresion.SelectedRow.Cells(4).Text.ToString
        Dim strPeriodo As String = gv_impresion.SelectedRow.Cells(7).Text.ToString
        Try
            ViewState("POA") = Server.HtmlDecode(hfPOA.Value.ToString)
            ViewState("Nombre") = Server.HtmlDecode(strNombre)
            ViewState("Ejecutor") = Server.HtmlDecode(hfEjecutor.Value.ToString)
            ViewState("Eje_Des") = Server.HtmlDecode(strEjecutor)
            ViewState("PPER") = Server.HtmlDecode(strPPER.Text)
            ViewState("PPER_ID") = Server.HtmlDecode(hfPPER.Value.ToString)
            ViewState("PERIODO") = Server.HtmlDecode(strPeriodo)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Protected Sub btnAgregar_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnAgregar.Click
        Dim objReporte As ReporteBC = New ReporteBC
        If gv_impresion.SelectedIndex > -1 Then
            If ViewState("PERIODO").ToString = "Ingresado" Then
                alerta.Mostrar_Alerta("Aviso", "El Avance Mensual ya fué ingresado")
            Else
                'captura de estado
                Dim objGeneral As GeneralBC
                Try
                    objGeneral = New GeneralBC
                    ViewState("Estado") = objGeneral.EstadoFechaMensual(ddlPeriodo.SelectedValue.ToString)
                Catch ex As Exception
                    Throw ex
                End Try
                'si estado es 1 redireccionar a detalle de avance nuevo
                If ViewState("Estado") = "1" Then
                    Response.Redirect("AFinancieroMensual.aspx?Nombre=" + ViewState("Nombre").ToString + "&PPER=" + ViewState("PPER").ToString + _
                                  "&PPER_ID=" + ViewState("PPER_ID").ToString + "&Ejecutor=" + ViewState("Ejecutor").ToString + _
                                  "&Eje_Des=" + ViewState("Eje_Des") + "&POA=" + ViewState("POA").ToString + "&CodPeriodo=" + _
                                  ddlPeriodo.SelectedValue.ToString + "&Periodo=" + ddlPeriodo.SelectedItem.Text + "&Nuevo=1&Persona=" + Session("Persona"))

                Else
                    alerta.Mostrar_Alerta("Aviso", "Solo se puede ingresar datos del periodo actual")
                End If
            End If
        Else
            alerta.Mostrar_Alerta("Aviso", "Debe seleccionar un registro")
        End If
    End Sub

    Protected Sub txt_numero_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_numero.TextChanged
        CargarGrilla(txt_numero.Text.ToString, ddlTipo.SelectedValue.ToString, ddlPeriodo.SelectedValue.ToString)
    End Sub

    Protected Sub ddlTipo_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlTipo.SelectedIndexChanged
        CargarGrilla(txt_numero.Text.ToString, ddlTipo.SelectedValue.ToString, ddlPeriodo.SelectedValue.ToString)
    End Sub

    Protected Sub ddlPeriodo_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlPeriodo.SelectedIndexChanged
        CargarGrilla(txt_numero.Text.ToString, ddlTipo.SelectedValue.ToString, ddlPeriodo.SelectedValue.ToString)
    End Sub

    Protected Sub btnModificar_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnModificar.Click
        Dim objReporte As ReporteBC = New ReporteBC
        If gv_impresion.SelectedIndex > -1 Then
            If ViewState("PERIODO").ToString <> "Ingresado" Then
                alerta.Mostrar_Alerta("Aviso", "El Avance Mensual no fué ingresado")
            Else
                'captura de estado
                Dim objGeneral As GeneralBC
                Try
                    objGeneral = New GeneralBC
                    ViewState("Estado") = objGeneral.EstadoFechaMensual(ddlPeriodo.SelectedValue.ToString)
                Catch ex As Exception
                    Throw ex
                End Try
                'si estado es 1 redireccionar a avance detalle edicion
                If ViewState("Estado") = "1" Then
                    Response.Redirect("AFinancieroMensual.aspx?Nombre=" + ViewState("Nombre").ToString + "&PPER=" + ViewState("PPER").ToString + _
                                  "&PPER_ID=" + ViewState("PPER_ID").ToString + "&Ejecutor=" + ViewState("Ejecutor").ToString + _
                                  "&Eje_Des=" + ViewState("Eje_Des") + "&POA=" + ViewState("POA").ToString + "&CodPeriodo=" + _
                                  ddlPeriodo.SelectedValue.ToString + "&Periodo=" + ddlPeriodo.SelectedItem.Text + "&Nuevo=0&Persona=" + ViewState("Persona"))
                Else
                    alerta.Mostrar_Alerta("Aviso", "Solo se puede ingresar datos del periodo actual")
                End If
            End If
        Else
            alerta.Mostrar_Alerta("Aviso", "Debe seleccionar un registro")
        End If
    End Sub

    Protected Sub btnExportar_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnExportar.Click
        ExportarGrilla(gv_doc)
    End Sub

    Private Sub ExportarGrilla(ByVal grilla As GridView)
        Dim sb As StringBuilder = New StringBuilder()
        Dim sw As StringWriter = New StringWriter(sb)
        Dim htw As HtmlTextWriter = New HtmlTextWriter(sw)
        Dim pagina As Page = New Page
        'CREAR TITULO
        Dim Tabla As New Table
        Dim Celda As New TableCell()
        Celda.RowSpan = 1
        Celda.ColumnSpan = 19
        Celda.Text = "LISTA DE AVANCE FINANCIERO MENSUAL"
        Celda.Font.Name = "Calibri"
        Celda.Font.Size = 12
        Celda.Font.Bold = True
        Celda.ForeColor = Drawing.Color.Black
        Celda.BackColor = Drawing.Color.White
        Celda.HorizontalAlign = HorizontalAlign.Center
        Celda.VerticalAlign = VerticalAlign.Middle
        Dim Fila As New TableRow()
        Fila.Cells.Add(Celda)
        Tabla.Rows.AddAt(0, Fila)
        '------------
        Dim form = New HtmlForm
        pagina.EnableEventValidation = False
        pagina.DesignerInitialize()
        pagina.Controls.Add(form)
        form.Controls.Add(Tabla)
        form.Controls.Add(grilla)
        pagina.RenderControl(htw)
        Response.Clear()
        Response.Buffer = True
        Response.ContentType = "application/vnd.ms-excel"
        Response.AddHeader("Content-Disposition", "attachment;filename=AVANCE_FINANCIERO_MENSUAL_" + Now.Date.Year.ToString + Now.Date.Month.ToString + Now.Date.Day.ToString + Now.Hour.ToString + Now.Minute.ToString + Now.Second.ToString + ".xls")
        Response.Charset = "UTF-8"
        Response.ContentEncoding = Encoding.Default
        Response.Write(sb.ToString())
        Response.End()
    End Sub

    Protected Sub gv_doc_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv_doc.RowDataBound
        'dar formato a celdas
        If e.Row.RowType = DataControlRowType.DataRow Then
            e.Row.BackColor = Drawing.Color.FromArgb(255, 255, 255)
            If e.Row.Cells(7).Text = "&nbsp;" Then
                e.Row.Cells(7).Text = "Falta ingresar"
                e.Row.Cells(7).ForeColor = Drawing.Color.Red
            Else
                e.Row.Cells(7).Text = "Ingresado"
            End If
            Select Case e.Row.Cells(6).Text
                Case "1"
                    e.Row.Cells(6).BackColor = Drawing.Color.FromArgb(0, 176, 80)
                Case "2"
                    e.Row.Cells(6).BackColor = Drawing.Color.FromArgb(255, 192, 0)
                Case "3"
                    e.Row.Cells(6).BackColor = Drawing.Color.FromArgb(255, 0, 0)
            End Select
            e.Row.Cells(6).Text = ""
        End If
    End Sub

End Class



